<?php
class kwitansipembelian_model extends CI_Model{
	private $NoKwitansi;
    private $NoFaktur;
    private $Kodesupplier;
	private $suppliername;
  	private $TotalQuantity;
	private $TotalAmount;
	private $Pay_amount;
    private $Typebayar;
    private $Keterangan;
	
	private $KodeDetailBarang;
	private $Productname;
	private $Quantity;
    private $Discount;
    private $Amount;
	
    private $Createdby;
    private $Createdon;
    private $Lastupdateby;
  	private $Lastupdateon;
  	private $Active;
	
	function setNoKwitansi($nokwitansi){
		$this->NoKwitansi = $nokwitansi;
	}
	function getNoKwitansi(){
		return $this->NoKwitansi;
	}
	
	function setNoFaktur($nofaktur){
		$this->NoFaktur = $nofaktur;
	}
	function getNoFaktur(){
		return $this->NoFaktur;
	}
	
	function setKodesupplier($kdsupp){
		$this->Kodesupplier = $kdsupp;
	}
	function getKodesupplier(){
		return $this->Kodesupplier;
	}
	
	function setsuppliername($suppliername){
		$this->suppliername = $suppliername;
	}
	function getsuppliername(){
		return $this->suppliername;
	}
	
	function setTotalQuantity($ttlqty){
		$this->TotalQuantity = $ttlqty;
	}
	function getTotalQuantity(){
		return $this->TotalQuantity;
	}
	
	function setTotalAmount($ttlamount){
		$this->TotalAmount = $ttlamount;
	}
	function getTotalAmount(){
		return $this->TotalAmount;
	}
	
	function setPay_amount($Pay_amount){
		$this->Pay_amount = $Pay_amount;
	}
	function getPay_amount(){
		return $this->Pay_amount;
	}
	
	function setTypebayar($tipebyr){
		$this->Typebayar = $tipebyr;
	}
	function getTypebayar(){
		return $this->Typebayar;
	}
	
	function setKeterangan($ket){
		$this->Keterangan = $ket;
	}
	function getKeterangan(){
		return $this->Keterangan;
	}
	
	function setKodeDetailBarang($kdbarangdet){
		$this->KodeDetailBarang = $kdbarangdet;
	}
	function getKodeDetailBarang(){
		return $this->KodeDetailBarang;
	}
	
	function setProductname($Productname){
		$this->Productname = $Productname;
	}
	function getProductname(){
		return $this->Productname;
	}
	
	function setQuantity($QTY){
		$this->Quantity = $QTY;
	}
	function getQuantity(){
		return $this->Quantity;
	}
	
	function setDiscount($discount){
		$this->Discount = $discount;
	}
	function getDiscount(){
		return $this->Discount;
	}
	
	function setAmount($amount){
		$this->Amount = $amount;
	}
	function getAmount(){
		return $this->Amount;
	}
	
	function setCreatedBy($c_by){
		$this->CreatedBy = $c_by;
	}
	function getCreatedBy(){
		return $this->CreatedBy;
	}

	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->CreatedOn = $dateNow;
	}
	function getCreatedOn(){
		return $this->CreatedOn;
	}

	function setLastUpdateBy($u_by){
		$this->LastUpdateBy = $u_by;
	}
	function getLastUpdateBy(){
		return $this->LastUpdateBy;
	}

	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->LastUpdateOn = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->LastUpdateOn;
	}

	function setActive($active_val){
		$this->Active = $active_val;
	}
	function getActive(){
		return $this->Active;
	}
	
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}
	
	function ambilnokwitansi()
	{
		$query = $this->db->query("select MAX(RIGHT(NoKwitansi,4)) as kdkwitansi from kwitansi");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idkwitansi)
			{
				$tmpkwitansi = ((int)$idkwitansi->kdkwitansi)+1;
				$kd = sprintf("%04s", $tmpkwitansi);
			}
			
		}
		else
		{
			$kd = "0001";
		}
		
		$kd = "KWP".$kd; 
		
		return $kd;
	}
	
	function insert()
	{
		$data = array(
			'NoKwitansi' => $this->getNoKwitansi(),
			'TotalQuantity' => $this->getTotalQuantity(),
			'TotalAmount' => $this->getTotalAmount(),
			'Pay_amount' => $this->getPay_amount(),
			'Typebayar' => $this->getTypebayar(),
			'Keterangan' => $this->getKeterangan(),
			'Createdby' => $this->getCreatedBy(),
			'Createdon' => $this->getCreatedOn(),
			'Lastupdateby' => 'null',
			'Lastupdateon' => 'null',
			'Active' => $this->getActive()						
		);
		$this->db->insert('kwitansi',$data);
	}
	
	function insertdetail()
	{
		$data1 = array(
			'NoKwitansi' => $this->getNoKwitansi(),
			'NoFaktur' => $this->getNoFaktur(),
			'Quantity' => $this->getQuantity(),
			'Discount' => '0',
			'Ppn' => '0',
			'Amount' => $this->getAmount()					
		);
		$this->db->insert('transaction_kwitansi',$data1);
	}
	
	function update()
	{
		$data = array(
			'TotalQuantity' => $this->getTotalQuantity(),
			'TotalAmount' => $this->getTotalAmount(),
			'Pay_amount' => $this->getPay_amount(),
			'Typebayar' => $this->getTypebayar(),
			'Keterangan' => $this->getKeterangan(),
			'Lastupdateby' => $this->getLastupdateby(),
			'Lastupdateon' => $this->getLastupdateon()		
		);
		$this->db->where(array('NoKwitansi' => $this->getNoKwitansi()));
        $this->db->update('kwitansi', $data);
	}
	
	function updatedetail()
	{
		$data1 = array(
			'NoKwitansi' => $this->getNoKwitansi(),
			'NoFaktur' => $this->getNoFaktur(),
			'Quantity' => $this->getQuantity(),
			'Discount' => $this->getDiscount(),
			'Ppn' => '0',
			'Amount' => $this->getAmount()					
		);
		$this->db->where(array('NoKwitansi' => $this->getNoKwitansi()));
        $this->db->update('transaction_kwitansi', $data);
	}
	
	function deletekwitansi() {
        $data = array(
            'Active' => '0'
        );
        $this->db->where(array('NoKwitansi' => $this->getNoKwitansi()));
        $this->db->update('kwitansi', $data);
    }
	
	function getlistnofaktur()
	{
		$this->db->select("faktur_product_pembelian.NoFaktur,faktur_product_pembelian.DateFaktur,
		detail_product.KodeDetailBarang,detail_product.Productname,
		transaction_faktur_product_pembelian.Quantity,
		transaction_faktur_product_pembelian.Amount,
		transaction_faktur_product_pembelian.IDdetail,
		supplier.Kodesupplier,supplier.supplierName")
        ->join('transaction_faktur_product_pembelian', 'transaction_faktur_product_pembelian.NoFaktur=faktur_product_pembelian.NoFaktur')
		->join('order_product', 'order_product.NoPo=transaction_faktur_product_pembelian.NoPo')
		->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')
		->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_pembelian.KodeDetailBarang')
		->where(array('faktur_product_pembelian.NoFaktur' => $this->getNoFaktur()))
		->group_by(array('faktur_product_pembelian.NoFaktur','transaction_faktur_product_pembelian.KodeDetailBarang'));
        $query = $this->db->get('faktur_product_pembelian');
        return $query->result();
	}
	
	function getnofakturlike()
	{
		$this->db->select("faktur_product_pembelian.NoFaktur,faktur_product_pembelian.DateFaktur,
		detail_product.KodeDetailBarang,detail_product.Productname,
		transaction_faktur_product_pembelian.Quantity,
		transaction_faktur_product_pembelian.Amount,
		transaction_faktur_product_pembelian.IDdetail,
		supplier.Kodesupplier,supplier.supplierName")
        ->join('transaction_faktur_product_pembelian', 'transaction_faktur_product_pembelian.NoFaktur=faktur_product_pembelian.NoFaktur')
		->join('order_product', 'order_product.NoPo=transaction_faktur_product_pembelian.NoPo')
		->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')
		->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_pembelian.KodeDetailBarang')
		->where(array('faktur_product_pembelian.Active'=>'1','transaction_faktur_product_pembelian.Active'=>'1'))->like(array('faktur_product_pembelian.NoFaktur' => $this->getNoFaktur()))
		->group_by(array('faktur_product_pembelian.NoFaktur','transaction_faktur_product_pembelian.KodeDetailBarang'));
        $query = $this->db->get('faktur_product_pembelian');
        return $query->result();
	}
	
	function getList()
	{
		//order_product.Kodesupplier,supplier.suppliername,
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("faktur_product_pembelian.NoFaktur,faktur_product_pembelian.DateFaktur,
		detail_product.KodeDetailBarang,detail_product.Productname,
		transaction_faktur_product_pembelian.Quantity,
		transaction_faktur_product_pembelian.Amount,
		transaction_faktur_product_pembelian.IDdetail,
		supplier.Kodesupplier,supplier.supplierName")
        ->join('transaction_faktur_product_pembelian', 'transaction_faktur_product_pembelian.NoFaktur=faktur_product_pembelian.NoFaktur')
		->join('order_product', 'order_product.NoPo=transaction_faktur_product_pembelian.NoPo')
		->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')
		->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_pembelian.KodeDetailBarang')
		->where(array('faktur_product_pembelian.Active'=>'1','transaction_faktur_product_pembelian.Active'=>'1','faktur_product_pembelian.DateFaktur'=>$tanggal))
		->group_by(array('faktur_product_pembelian.NoFaktur','transaction_faktur_product_pembelian.KodeDetailBarang'));
        $query = $this->db->get('faktur_product_pembelian');
        return $query->result();
	}
	
	function getListlimit($limit,$start)
	{
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("faktur_product_pembelian.NoFaktur,faktur_product_pembelian.DateFaktur,
		detail_product.KodeDetailBarang,detail_product.Productname,
		transaction_faktur_product_pembelian.Quantity,
		transaction_faktur_product_pembelian.Amount,
		transaction_faktur_product_pembelian.IDdetail,
		supplier.Kodesupplier,supplier.supplierName")
        ->join('transaction_faktur_product_pembelian', 'transaction_faktur_product_pembelian.NoFaktur=faktur_product_pembelian.NoFaktur')
		->join('order_product', 'order_product.NoPo=transaction_faktur_product_pembelian.NoPo')
		->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')
		->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_pembelian.KodeDetailBarang')
		->where(array('faktur_product_pembelian.Active'=>'1'))
		->limit($limit,$start)
		->group_by(array('faktur_product_pembelian.NoFaktur','transaction_faktur_product_pembelian.KodeDetailBarang'));
        $query = $this->db->get('faktur_product_pembelian');
        return $query->result();
	}
	
	function countGetList()
	{
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("faktur_product_pembelian.NoFaktur,faktur_product_pembelian.DateFaktur,
		detail_product.KodeDetailBarang,detail_product.Productname,
		transaction_faktur_product_pembelian.Quantity,
		transaction_faktur_product_pembelian.Amount,
		transaction_faktur_product_pembelian.IDdetail,
		supplier.Kodesupplier,supplier.supplierName")
        ->join('transaction_faktur_product_pembelian', 'transaction_faktur_product_pembelian.NoFaktur=faktur_product_pembelian.NoFaktur')
		->join('order_product', 'order_product.NoPo=transaction_faktur_product_pembelian.NoPo')
		->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')
		->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_pembelian.KodeDetailBarang')
		->where(array('faktur_product_pembelian.Active'=>'1'))
		->group_by(array('faktur_product_pembelian.NoFaktur','transaction_faktur_product_pembelian.KodeDetailBarang'));
        $query = $this->db->get('faktur_product_pembelian');
        return $query->num_rows();
	}
	
	function ambilnofaktur()
	{
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select()
		//->where(array('faktur_product_pembelian.Active'=>'1','faktur_product_pembelian.DateFaktur'=>$tanggal));
		->where(array('faktur_product_pembelian.Active'=>'1'));
        $query = $this->db->get('faktur_product_pembelian');
        return $query->result();
	}
	
	function getListfakturkwt(){
		$this->db->select("kwitansi.NoKwitansi,transaction_kwitansi.NoFaktur,
		transaction_faktur_product_pembelian.KodeDetailBarang,
		detail_product.Productname,detail_product.Typeunit,transaction_kwitansi.Quantity,
		transaction_kwitansi.Discount,transaction_kwitansi.Amount")
		->join('kwitansi', 'kwitansi.NoKwitansi=transaction_kwitansi.NoKwitansi')
		->join('transaction_faktur_product_pembelian', 'transaction_faktur_product_pembelian.NoFaktur=transaction_kwitansi.NoFaktur')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_pembelian.KodeDetailBarang')
		->where(array('transaction_kwitansi.NoKwitansi' => $this->getNoKwitansi()))
		->group_by(array('transaction_faktur_product_pembelian.NoFaktur'));
        $query = $this->db->get('transaction_kwitansi');
        return $query->result();
	}
	
	function ambilnokwitansi1() {
        $this->db->select()
       	->where(array('kwitansi.NoKwitansi' => $this->getNoKwitansi()));
        $query = $this->db->get('kwitansi');
        return $query->result();
    }
	
	function ambilsupplier() 
	{
        $this->db->select("order_product.Kodesupplier,supplier.suppliername")
		->join('transaction_faktur_product_pembelian', 'transaction_faktur_product_pembelian.NoPo=order_product.NoPo')
		->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')
       	->where(array('transaction_faktur_product_pembelian.NoFaktur' => $this->getNoFaktur(),'transaction_faktur_product_pembelian.Active'=>'1'))
		->group_by(array('order_product.NoPo'));
        $query = $this->db->get('order_product');
        return $query->result();
    }
}